/*
 * @描述: 菜单管理数据请求
 * @版权所有: 广东国星科技有限公司
 * @商业授权: www.mscodecloud.com
 */
import { querySysMenu, deleteSysMenu, deleteDataField, addSysMenu, exportSysMenu, querySysMenuTree, queryMenuButtonCheckbox, queryCheckedMenuButton, authorizeMenuDataToRole } from '@/services/api';

export default {
  namespace: 'sysMenu',

  state: {
    data: {
      list: [],
      pagination: {}
    },
    treeData: [],
    checkboxData: [],
    checkedMenuButton: []
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(querySysMenu, payload);
      yield put({
        type: 'save',
        payload: response
      });
    },
    *fetchTree({ payload }, { call, put }) {
      const response = yield call(querySysMenuTree, payload);
      yield put({
        type: 'saveTree',
        payload: response
      });
    },
    *fetchCheckbox({ payload }, { call, put }) {
      const response = yield call(queryMenuButtonCheckbox, payload);
      yield put({
        type: 'saveCheckbox',
        payload: response
      });
    },
    *fetchCheckedMenuButton({ payload }, { call, put }) {
      const response = yield call(queryCheckedMenuButton, payload);
      yield put({
        type: 'saveCheckedMenuButton',
        payload: response
      });
    },
    *add({ payload }, { call, put }) {
      const response = yield call(addSysMenu, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *authorizeMenuData({ payload }, { call, put }) {
      const response = yield call(authorizeMenuDataToRole, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *delete({ payload }, { call, put }) {
      const response = yield call(deleteSysMenu, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *deleteDataPermission({ payload }, { call, put }) {
      const response = yield call(deleteDataField, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *export({ payload }, { call, put }) {
      const response = yield call(exportSysMenu, payload);
      yield put({
        type: 'save',
        payload: response
      });
      return response;
    }
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload.data
      };
    },
    saveTree(state, action) {
      return {
        ...state,
        treeData: action.payload.data
      };
    },
    saveCheckbox(state, action) {
      return {
        ...state,
        checkboxData: action.payload.data
      };
    },
    saveCheckedMenuButton(state, action) {
      return {
        ...state,
        checkedMenuButton: action.payload.data
      };
    }
  }
};
